<?php

/**
 * @author Ivan Kukic
 * Date: May 12, 2010 6:06:51 PM
 */
class posts_m extends Model {

    var $errors = "";
    var $data = "";
    var $maximgwidth = 450;
    var $table = "news";

    function posts_m () {
        parent::Model();
        $this->setData('path','./_cms_adminstaff/');
        $this->setData('is_image',FALSE);
    }// construct of news

    function setData($key,$value) {
        $this->data[$key] = $value;
    }

    function getData($key) {
        return $this->data[$key];
    }

    function create() {
        if($this->validate()) {
            // check is need to upload photo
            if($this->input->post('is_image')) {
                // now check upload file and image resize
                $this->setData('is_image',TRUE);
                if($this->checkUpload() == FALSE) {
                    return FALSE;
                }
            }

            // if all ok then add data to db and resize image if you need
            if($this->getData('is_image')) $this->_resize_image(); // resize image if checked

            $data['title'] = $this->input->post('title');
            $data['content'] = $this->input->post('content');
            $data['lang'] = $this->input->post('lang');
            $data['title'] = $this->input->post('title');
            if($this->getData('is_image')) {
                $imgdata = $this->getData('imgdata');
                $data['is_image'] = 1;
                $data['image_ext'] = $imgdata['file_ext'];
            }
            $data['update_date'] = time();
            $data['publish_date'] = strtotime($this->input->post('publish_date'));
            $data['date'] = time();

            $this->db->insert($this->table,$data);
            if($this->getData('is_image')) {
                $this->rename_image($this->db->insert_id());
            }
            return TRUE;
        }else {
            $this->session->set_flashdata('errors',$this->errors);
            return FALSE;
        }
    }

    function read($id) {
        return $this->db->where('id',$id)->get($this->table)->row_array();
    }

    function readAll($lang = NULL) {
        if($lang == NULL) {
            return $this->db->order_by('id','desc')->get($this->table)->result_array();
        }else {
            return $this->db->where('lang',$lang)->order_by('id','desc')->get($this->table)->result_array();
        }
    }

    function update() {
        if($this->validate()) {
            // check is need to upload photo
            if($this->input->post('upload_another') || $this->input->post('is_image')) {
                
                $this->setData('is_image',TRUE);
                if($this->checkUpload() == FALSE) {
                    return FALSE;
                }
            }

            // if all ok then add data to db and resize image if you need
            if($this->getData('is_image')) $this->_resize_image(); // resize image if checked

            $data['title'] = $this->input->post('title');
            $data['content'] = $this->input->post('content');
            $data['lang'] = $this->input->post('lang');
            $data['title'] = $this->input->post('title');
            if($this->getData('is_image')) {
                $imgdata = $this->getData('imgdata');
                $data['is_image'] = 1;
                $data['image_ext'] = $imgdata['file_ext'];
            }

            if($this->input->post('delete_image')){
                $data['is_image'] = 0;
            }
            $data['update_date'] = time();
            $data['publish_date'] = strtotime($this->input->post('publish_date'));
            $this->db->where('id',$this->input->post('id'));
            $this->db->update($this->table, $data);

            if($this->getData('is_image')) {
                $this->rename_image($this->input->post('id'));
            }
            
            return TRUE;
        }else {
            $this->session->set_flashdata('errors',$this->errors);
            return FALSE;
        }
    }

    function delete($id) {
        if($id != NULL) {
            $this->db->where('id',$id)->delete($this->table);
            echo json_encode(array('success'=>'success'));
        }
    }

    function validate() {
        $this->form_validation->set_rules('title','Post title','trim|required');
        $this->form_validation->set_rules('publish_date','Post publish date','trim|required');
        $this->form_validation->set_rules('lang','Language','trim|required');
        $this->form_validation->set_rules('content','Post content','trim|required');
        $this->form_validation->set_rules('is_image','Is Image','trim');

        if($this->form_validation->run()) {
            return TRUE;
        }else {
            $this->errors = validation_errors('<li>', '</li>');
            return FALSE;
        }
    }

    function checkUpload() {
        $config['upload_path']      = $this->getData('path');
        $config['allowed_types']    = 'gif|jpg|png|jpeg';
        $config['max_size']         = 1024;
        $config['max_width']        = 5000;
        $config['max_height']       = 5000;
        $config['overwrite']	= TRUE;
        $config['file_name'] = md5(time().'_'.time());
        $this->upload->initialize($config);

        if($this->upload->do_upload('pimage')) {
            $this->setData('imgdata',$this->upload->data());
            return TRUE;
        }else {
            $this->errors .= $this->upload->display_errors();
            return FALSE;
        }
    }

    function _resize_image() {
        $imgdata = $this->getData('imgdata');
        if($imgdata['image_width'] > 450) {
            $config['image_library']    = 'gd2';
            $config['source_image']     = $imgdata['full_path'];
            $config['create_thumb']     = FALSE;
            $config['maintain_ratio']   = TRUE;
            $config['width']            = $this->maximgwidth;
            $config['height']           = ($this->maximgwidth * $imgdata['image_height']) / $imgdata['image_height'];

            $this->image_lib->initialize($config);
            $this->image_lib->resize();
        }
    }

    function rename_image($id) {
        $imgdata = $this->getData('imgdata');
        if(file_exists($imgdata['file_path'].$id.$imgdata['file_ext'])) {
            rename($imgdata['file_path'].$id.$imgdata['file_ext'],$imgdata['file_path'].$id.time().$imgdata['file_ext']);
        }
        rename($imgdata['full_path'], $imgdata['file_path'].$id.$imgdata['file_ext']);
    }

}

?>